package com.pulse.common.utils.sor.make;

import com.pulse.common.utils.sor.domain.SorInfo;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;

public class FxdParamsMake {

    public static final String FIXED_PARAMS_BLOCK_ID = "FxdParams\0";

    public static ByteBuffer make(SorInfo info) {
        ByteBuffer buffer = ByteBuffer.allocate(256);
        buffer.order(ByteOrder.LITTLE_ENDIAN);

        // 块ID
        buffer.put(FIXED_PARAMS_BLOCK_ID.getBytes());

        // 写入时间戳
        String dateString = info.getFxdParams().getDateTime();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime localDateTime = LocalDateTime.parse(dateString,formatter);

//        // 将 LocalDateTime 转换为 UTC 时间
//        ZonedDateTime utcDateTime = localDateTime.atZone(ZoneId.systemDefault()) // 本地时区
//                .withZoneSameInstant(ZoneOffset.UTC); // 转换为 UTC 时区
//        // 如果需要转换为 Date 对象
//        Date date = Date.from(utcDateTime.toInstant());

        Date date = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
        long time = date.getTime()/1000; // 转换为秒
        buffer.putInt((int)time);
        // 写入距离单位
        buffer.put(info.getFxdParams().getUnit().substring(0,2).getBytes());
        // 写入实际波长
        Double waveLength = Double.parseDouble(info.getFxdParams().getWavelength().split(" ")[0]);
        int wl = (int)(waveLength*10);
        buffer.putShort((short)wl);
        // 采集偏移量
        String acquisitionOffsetStr = info.getFxdParams().getAcquisitionOffset() == null ? "0" : info.getFxdParams().getAcquisitionOffset();
        long acquisitionOffset = Long.parseLong(acquisitionOffsetStr);
        buffer.putInt((int)acquisitionOffset);
        // 采集偏移距离
        String acquisitionOffsetDistanceStr = info.getFxdParams().getAcquisitionOffsetDistance() == null ? "0" : info.getFxdParams().getAcquisitionOffsetDistance();
        Double acquisitionOffsetDist = Double.parseDouble(acquisitionOffsetDistanceStr)*10;
        buffer.putInt((int) Math.round(acquisitionOffsetDist));
        // 脉冲宽度条目数量
        int numberOfPulseWidthEntries = Integer.parseInt(info.getFxdParams().getNumberOfPulseWidthEntries());
        buffer.putShort((short)numberOfPulseWidthEntries);
        // 脉冲宽度
        String pulseWidthStr = info.getFxdParams().getPulseWidth() == null? "0" : info.getFxdParams().getPulseWidth().split(" ")[0];
        int pulseWidth = Integer.parseInt(pulseWidthStr);
        buffer.putShort((short)pulseWidth);
        // 采样间隔
        String sampleSpacingStr = info.getFxdParams().getSampleSpacing() == null? "0" : info.getFxdParams().getSampleSpacing().split(" ")[0];
        double sampleSpacing = Double.parseDouble(sampleSpacingStr)/1e-08;
        buffer.putInt((int)sampleSpacing);
        // 数据点数量
        String numDataPointsStr = info.getFxdParams().getNumDataPoints() == null ? "0" : info.getFxdParams().getNumDataPoints();
        long numDataPoints = Long.parseLong(numDataPointsStr);
        buffer.putInt((int)numDataPoints);
        // 折射率
        String indexStr = info.getFxdParams().getIndex() == null ? "0" : info.getFxdParams().getIndex();
        Double index = Double.parseDouble(indexStr) * 100000;
        buffer.putInt(index.intValue());
        // 背向散射系数
        String bcStr = info.getFxdParams().getBc() == null ? "0" : info.getFxdParams().getBc().split(" ")[0];
        Double bcD = Double.parseDouble(bcStr) * 10;
        long bc = Math.round(bcD);
        buffer.putShort((short)bc);
        // 平均次数
        String numAveragesStr = info.getFxdParams().getNumAverages() == null ? "0" : info.getFxdParams().getNumAverages();
        long numAverages = Long.parseLong(numAveragesStr);
        buffer.putInt((int)numAverages);
        // 平均时间(测试时间)
        String averagingTimeStr = info.getFxdParams().getAveragingTime() == null ? "0" :info.getFxdParams().getAveragingTime().split(" ")[0];
        int averagingTime = Integer.parseInt(averagingTimeStr);
        buffer.putShort((short)averagingTime);
        // 测试范围
        Double range = info.getFxdParams().getRange();
        buffer.putInt((int) Math.round(range));
        // 采集范围距离
        String acquisitionRangeDistanceStr = info.getFxdParams().getAcquisitionRangeDistance() == null ? "0" : info.getFxdParams().getAcquisitionRangeDistance();
        Double acquisitionRangeDistance = Double.parseDouble(acquisitionRangeDistanceStr);
        buffer.putInt((int) Math.round(acquisitionRangeDistance));
        // 前面板偏移量
        String frontPanelOffsetStr = info.getFxdParams().getFrontPanelOffset() == null ? "0" : info.getFxdParams().getFrontPanelOffset();
        long frontPanelOffset = Long.parseLong(frontPanelOffsetStr);
        buffer.putInt((int)frontPanelOffset);
        // 噪声底电平
        String noiseFloorLevelStr = info.getFxdParams().getNoiseFloorLevel() == null ? "0" : info.getFxdParams().getNoiseFloorLevel();
        int noiseFloorLevel = Integer.parseInt(noiseFloorLevelStr);
        buffer.putShort((short)noiseFloorLevel);
        // 噪声底缩放因子
        String noiseFloorScalingFactorStr = info.getFxdParams().getNoiseFloorScalingFactor() == null ? "0" : info.getFxdParams().getNoiseFloorScalingFactor();
        int noiseFloorScalingFactor = Integer.parseInt(noiseFloorScalingFactorStr);
        buffer.putShort((short)noiseFloorScalingFactor);
        // 第一个点的功率偏移
        String powerOffsetFirstPointStr = info.getFxdParams().getPowerOffsetFirstPoint() == null ? "0" : info.getFxdParams().getPowerOffsetFirstPoint();
        int powerOffsetFirstPoint = Integer.parseInt(powerOffsetFirstPointStr);
        buffer.putShort((short)powerOffsetFirstPoint);
        // 损耗阈值(非反射门限)
        String lossThrStr = info.getFxdParams().getLossThr() == null ? "0" : info.getFxdParams().getLossThr().split(" ")[0];
        Double lossThrD = Double.parseDouble(lossThrStr) * 1000;
        short lossThr = (short) Math.round(lossThrD);
        buffer.putShort(lossThr);
        // 反射阈值（反射门限）
        String reflThrStr = info.getFxdParams().getReflThr() == null ? "0" : info.getFxdParams().getReflThr().split(" ")[0];
        Double reflThrD = Math.abs(Double.parseDouble(reflThrStr) * 1000);
        long reflThr = Math.round(reflThrD);
        buffer.putShort( (short) reflThr);
        // 结束门限
        String eotThrStr = info.getFxdParams().getEotThr() == null ? "0" : info.getFxdParams().getEotThr().split(" ")[0];
        Double eotThrD = Double.parseDouble(eotThrStr) * 1000;
        short eotThr = eotThrD.shortValue();
        buffer.putShort(eotThr);
        // 跟踪类型
        String traceType = info.getFxdParams().getTraceType() == null ? "ST" : info.getFxdParams().getTraceType().substring(0,2);
        buffer.put(traceType.getBytes());
        // x1
        String x1Str = info.getFxdParams().getX1() == null ? "0" : info.getFxdParams().getX1();
        int x1 = Integer.parseInt(x1Str);
        buffer.putInt(x1);
        // y1
        String y1Str = info.getFxdParams().getY1() == null ? "0" : info.getFxdParams().getY1();
        int y1 = Integer.parseInt(y1Str);
        buffer.putInt(y1);
        // x2
        String x2Str = info.getFxdParams().getX2() == null ? "0" : info.getFxdParams().getX2();
        int x2 = Integer.parseInt(x2Str);
        buffer.putInt(x2);
        // y2
        String y2Str = info.getFxdParams().getY2() == null ? "0" : info.getFxdParams().getY2();
        int y2 = Integer.parseInt(y2Str);
        buffer.putInt(y2);

        return buffer;
    }
}
